
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Ixyu.Base.Dictionaries;
using Ixyu.Base.Dictionaries.Dtos;
using Ixyu.Base.Dictionaries.DomainService;
using Ixyu.Base.Dictionaries.Authorization;


namespace Ixyu.Base.Dictionaries
{
    /// <summary>
    /// AbpDictionary应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize(AbpDictionaryPermissions.Node)]
    public class AbpDictionaryAppService : IxyuAppServiceBase
    {
        private readonly IRepository<AbpDictionary, int> _entityRepository;

        private readonly IAbpDictionaryManager _entityManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public AbpDictionaryAppService(
        IRepository<AbpDictionary, int> entityRepository
        ,IAbpDictionaryManager entityManager
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
        }


        /// <summary>
        /// 获取AbpDictionary的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		
        public async Task<PagedResultDto<AbpDictionaryListDto>> GetPaged(GetAbpDictionarysInput input)
		{

		    var query = _entityRepository.GetAll();
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<AbpDictionaryListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<AbpDictionaryListDto>>();

			return new PagedResultDto<AbpDictionaryListDto>(count,entityListDtos);
		}
        public async Task<dynamic> GetTree()
        {
            var query = _entityRepository.GetAll();
            // TODO:根据传入的参数添加过滤条件
            var entityList = await query.Where(u => u.Type < 2).Select(u=>new
            {
                Title=u.Key,
                u.Id,
                u.ParentId,
                u.Type,
                u.IsStatic
            }).ToListAsync();
            return entityList;
        }

        /// <summary>
        /// 通过指定id获取AbpDictionaryListDto信息
        /// </summary>
        public async Task<AbpDictionaryListDto> GetById(int id)
		{
			var entity = await _entityRepository.GetAsync(id);

		    return entity.MapTo<AbpDictionaryListDto>();
		}



		/// <summary>
		/// 添加或者修改AbpDictionary的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>

		/// <summary>
		/// 新增AbpDictionary
		/// </summary>
		public  async Task<AbpDictionaryEditDto> Create(AbpDictionaryEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <AbpDictionary>(input);
            var entity=input.MapTo<AbpDictionary>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<AbpDictionaryEditDto>();
		}

        /// <summary>
        /// 编辑AbpDictionary
        /// </summary>
        public  async Task Update(AbpDictionaryEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}

		/// <summary>
		/// 删除AbpDictionary信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<int> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}
    }
}


